﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Texfina.Core.Web;
using Texfina.Core.Common;
using Texfina.Entity;
using Texfina.Entity.Co;
using System.Text;
using Texfina.BOQry.Co;
using System.IO;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
namespace CO
{
    public partial class wfOrdenCompraRpt :PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        public override void ExportReport(string key)
        {
            DataSet ds = (DataSet)this.GetDataReport(key);
            //if (ds.Tables[0].Rows.Count == 0)
            //{
            //    this.hdnMsgOPB.Value = this.CreatePostbackMessage("ATENCIÓN", "No se encotraron Resultados", TypeMessageAlert.atention, false, "");
            //    return;
            //}
            ExcelHelper.ToExcel(ds, "Data.xls", Response);

        }
        
        public override object GetDataReport(string key)
        {

            EParOCFillrpt objE = new EParOCFillrpt();
           // RHBORpt.IPersonal objBO = (RHBORpt.IPersonal)WCFHelper.GetObject<RHBORpt.IPersonal>(typeof(RHBORpt.Personal));
            //objE.IdEmpresa = Session[Constants.ConstSessionVar.EMPRESA].ToString();
            objE.IdEmpresa = "001";

            objE.IdAnio = (this.txtAnio.Text);

            objE.IdMes =(dpMes.Text);


            BOrdenCompra objOC= new BOrdenCompra();
            DataTable dt = objOC.OrderCompra_rpt01(objE);
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            return ds;
         
        }
        public override void PrintReport(string key)
        {
      
            DataSet ds = (DataSet)this.GetDataReport(key);
            DataTable dt = new DataTable();
            dt = ds.Tables[0];
            if (dt.Rows.Count > 0)
            {
                MemoryStream[] msArray = new MemoryStream[dt.Rows.Count];

                DataTable dtClone = new DataTable();
                dtClone = dt.Clone();

                //string strIdEmpresa = "";
                //string straño = "";
                //string strmes = "";

                int x = 0;

                foreach (DataRow dr in dt.Rows)
                {
                    dtClone.ImportRow(dr);

                    //strIdEmpresa = dr["idTipoTrab"].ToString();
                    //straño = dr["stProrroga"].ToString();
                    //strmes = dr["stRemuMinima"].ToString();

                    ReportDocument objRpt = new ReportDocument();
                    objRpt.Load(this.MapPath(Constants2.ConstSistema.ROOT_FOLDER_PATH + "/co_orden_compra_qry_rpt.rpt"));
               
                            //strMsg = "No tiene Registros";
                            //this.hdnMsgOPB.Value = this.CreatePostbackMessage("ATENCIÓN", "No se encotraron Resultados", TypeMessageAlert.atention, false, "");
                            //break;
                     objRpt.SetDataSource(dtClone);
                    MemoryStream ms = new MemoryStream();

                    Response.Clear();

                    ms = (MemoryStream)objRpt.ExportToStream(ExportFormatType.PortableDocFormat);
                    msArray[x] = ms;
                    objRpt.Dispose();
                    objRpt = null;
                    dtClone.Clear();
                    x++;
               }

  
                MemoryStream msRes = ExcelHelper.MergePDFFiles(msArray);

                Response.AddHeader("Content-Disposition", "attachment;filename=CONTRATOS.pdf");

                Response.Buffer = true;
                Response.ContentType = "application/pdf";
                Response.OutputStream.Write(msRes.GetBuffer(), 0, msRes.GetBuffer().Length);
                Response.OutputStream.Flush();
                Response.OutputStream.Close();
                msRes.Close();
                Response.Flush();
                Response.End();
            }
            else
            {

                //strMsg = "No Existen Registros";
                //this.hdnMsgOPB.Value = this.CreatePostbackMessage("ATENCIÓN", strMsg, TypeMessageAlert.atention, false, "");
            }
       }
        protected void btnExportar_Click(object sender, EventArgs e)
        {
            ExportReport("");
        }

        protected void btnImprimir_Click(object sender, EventArgs e)
        {
            PrintReport("");
        }
    }
}